import {
    Controller, Post,
    UseInterceptors,
    UploadedFile, Session,
    Body,
    ParseIntPipe,
    UseGuards
} from '@nestjs/common';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FileInterceptor } from '@nestjs/platform-express';
import { UploadService } from './upload.service';
import { ResponseJson } from '@/common/type/app.dto';
import { ContainerService } from '@/container/container.service';
import { BusinessService } from '@/business/business.service';
import { AuthGuard } from '@/common/guard/auth.guard';
@Controller('upload')
@UseGuards(AuthGuard)
export class UploadController {
    constructor(
        private readonly uploadService: UploadService,
        private readonly containerservice: ContainerService,
        private readonly businessService: BusinessService,
    ) {}

    /**
     * 导入进场计划
     * @param file 
     * @param planID 
     * @param session 
     * @returns 
     */
    @Post('importinplans')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importinplans(
        @UploadedFile() file,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, number>> {
       
        return await this.containerservice.importInplans(
            file,
            
            session['user']['name'],
        );
    }



    /**
     * 
     * @param file 导入计划箱
     * @param session 
     * @returns 
     */
    @Post('importinplancntrs')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async upload(
        @UploadedFile() file,
        @Body('planID', ParseIntPipe) planID: number,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, number>> {
       
        return await this.containerservice.importCntrsFromExcel(
            file,
            planID,
            session['user']['name'],
        );
    }

    /**
     * 导入进场计划及箱号
     * @param file 
     * @param session 
     * @returns 
     */
    @Post('importinplanandcntr')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importinplanandcntr(
        @UploadedFile() file,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, number>> {
       
        return await this.containerservice.importPlanAndCntrPlus(
            file,
            session['user']['name'],
        );
    }

    /**
     * 导入计划出场箱
     * @param file 
     * @param planID 
     * @param session 
     * @returns 
     */
    @Post('importoutplancntrs')
    //@HttpCode(200)
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importgateout(
        @UploadedFile() file,
        @Body('planID', ParseIntPipe) planID: number,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, null>> {
        // console.log('file', file);
        return await this.containerservice.importGateOutFromExcel(
            file,
            planID,
            session['user']['name'],
        );
    }
    /**
     * 导入租箱计划出场箱
     * @param file 
     * @param planID 
     * @param session 
     * @returns 
     */
    @Post('importleaseplancntrs')
    //@HttpCode(200)
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importcntrlease(
        @UploadedFile() file,
        @Body('planID', ParseIntPipe) planID: number,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, null>> {
        // console.log('file', file);
        return await this.containerservice.importLeaseOutFromExcel(
            file,
            planID,
            session['user']['name'],
        );
    }
    /**
     * 导入租箱计划返场箱
     * @param file 
     * @param planID 
     * @param session 
     * @returns 
     */
    @Post('importleaseplanbackcntrs')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importcntrleaseback(
        @UploadedFile() file,
        @Body('planID', ParseIntPipe) planID: number,
        @Session() session: Record<string, any>,
    ): Promise<ResponseJson<null, null>> {
        return await this.containerservice.importLeaseBackFromExcel(
            file,
            planID,
            session['user']['name'],
        );
    }


    @Post('inventorycheck')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async inventorycheck(
        @UploadedFile() file,
        @Body('cfs_cod') cfs_cod: number,
    ): Promise<ResponseJson<any, null>> {
        
        return this.containerservice.inventoryCheck(file, cfs_cod)
        
    }

    /**
     * 导入应收付费用
     * @param file 
     * @param session 
     * @returns 
     */
    @Post('importfeedet')
    @UseInterceptors(FileInterceptor('file')) // 'file' 上传字段名称
    async importfeedet(
        @UploadedFile() file,
        @Session() session: Record<string, any>,
    ) {
        return this.businessService.importFeedet(file, session['user']['name']);
    }

    /* @Get('export')
    download(@Res() res: Response) {
        const url = join(__dirname, '../images/1697766952595.jpg');
        res.download(url);
    }

    @Get('stream')
    async down(@Res() res: Response) {
        const url = join(__dirname, '../images/1697766952595.jpg');
        const tarStream = new zip.Stream();
        await tarStream.addEntry(url);
        res.setHeader('Content-Type', 'application/octet-stream');
        res.setHeader(
            'Content-Disposition',
            'attachment; filename=xiaoman.jpg',
        );
        tarStream.pipe(res); // 返回zip文件
    } */
}
